/**
 * @developed by SilverBiology, LLC
 * @website http://www.silverbiology.com
*/
Ext.namespace('Taxamatch');

Taxamatch.AddAuthoirities = function(config) {

 	var dwcTpl = new Ext.XTemplate(
			'<tpl for="."><div class="search-item">'
		,	'<div class="dwca">{title}</div>'
		,	'<div>{url}</div>'
		,	'<div style="padding-top:3px">{description}</div>'
		,	'</div></tpl>'
	);

	this.dwcStore = new Ext.data.JsonStore({
			fields:[
					'id'
				,	'type'
				,	'url'
				,	'description'
				,	'title'
				,	'uuid'
			]
		,	url: 'resources/api/app.php'
		,	baseParams: {
				cmd: "getArchives"
			}
	});

	this.dwcStore.load();
	
	Ext.apply(this, config, {
			fileUpload: true
		,	labelWidth: 150
		,	url: 'resources/api/app.php'
		,	border: true
		,	bodyStyle: 'padding:15px'
		,	title: 'Add Authority'
		,	frame: true
		,	items: [{
					xtype: 'displayfield'
				,	value: 'This tool is used to add authority to database. For more information please see the Help section.'
				,	fieldLabel: 'Label'
				,	anchor: '100%'
				,	hideLabel: true
			}, {
					fieldLabel: "1) GBIF Published Archives"
				,	xtype: 'combo'	
				,	store: this.dwcStore
				,	mode: 'local'
				,	displayField: 'title'
				,	valueField: 'url'
				,	name: 'location'
				,	tpl: dwcTpl
				,	typeAhead: true
				,	forceSelection: true
				,	itemSelector: 'div.search-item'			
				,	ref: 'reflocation'
				,	triggerAction: 'all'
//				,	editable: false
				,	emptyText: ''
				,	anchor: '100%'
			},{
					fieldLabel: "2) Url to archive"
				,	name: 'directUrl'
				,	xtype: 'textfield'
				,	anchor: '100%'
				,	vtype: 'url'
				,	autoScroll: true
			},{
					xtype: 'fileuploadfield'
				,	id: 'form-file'
				,	emptyText: 'Select a file'
				,	fieldLabel: '3) or Upload DwC-A'
				,	name: 'dwcaFile'
				,	width: 200
			},{
					xtype: 'combo'	
				,	store: Ext.getCmp('db').store
				,	fieldLabel: 'Database'
				,	mode: 'local'
				,	name:'database'
				,	hiddenName:'database'
				,	allowBlank:false
				,	displayField: 'title' 
				,	valueField: 'id'
				,	typeAhead: false
				,	triggerAction: 'all'
				,	editable: false
				,	width: 200
			},{
					fieldLabel: "Authority Name"
				,	name: 'authority'
				,	xtype: 'textfield'
				,	ref: '../txtauthoname'
				,	width: 200
				,	allowBlank: false
				,	autoScroll: true
			}]
		,	buttons: [{
					text: 'Clear'
				,	scope: this
				,	handler: function(){
						this.getForm().reset();
						this.getForm().setValues({location: ''});
					}
			}, {
					text: 'Create Authority'
				,	scope: this
				,	handler: this.createAuthority
			}]			
	});
	
 	Taxamatch.AddAuthoirities.superclass.constructor.call(this, config);
   
} 

Ext.extend(Taxamatch.AddAuthoirities, Ext.form.FormPanel , {

	createAuthority: function(){
		var mask = new Ext.LoadMask( this.ownerCt.body, {
			msg: 'Creating Authority...'
		});
		var form = this.getForm().getValues();
		if (this.getForm().isValid() && (form.directUrl != '' || form.location != '' || Ext.getCmp('form-file').getValue() != '')){
			mask.show();
			if((!Ext.isEmpty(form.directUrl) || !Ext.isEmpty(form.location)) && Ext.isEmpty(Ext.getCmp('form-file').getValue())) {
				Ext.Ajax.request({
						url: 'resources/api/app.php'
					,	params: {
								cmd: "addAuthority"
							,	location: this.reflocation.value || form.directUrl
							,	database: form.database
							,	authority: form.authority	
						}	
					,	scope: this
					,	success: function(r) {
							mask.hide();
							var data = Ext.decode(r.responseText);
							if (data.success) {
								this.getForm().reset();
								Taxamatch.Notice.msg('Notice', 'Authority succesfully added.');
							} else {
								this.getForm().reset();
								Taxamatch.Notice.msg('Notice', data.error.msg);
							}
						}
					,	failure: function(r){
							Taxamatch.Notice.msg('Notice', 'Unknown error when adding authority.');
							mask.hide();
						}	
				}, this);
			} else {
				this.getForm().submit({
						url: 'resources/api/app.php'
					,	params: {
							cmd: "addAuthority"
						}
					,	scope: this	
					,	success: function(fp, o){
							mask.hide();
							var data = Ext.decode(o.response.responseText);
							if (data.success) {
								this.getForm().reset();
								Taxamatch.Notice.msg('Notice', 'Succesfully authority added.');
							} else {
								this.getForm().reset();
								Taxamatch.Notice.msg('Notice', 'Error in uploading.');
							}
						}
					,	failure: function(r){
							Taxamatch.Notice.msg('Notice', 'Error in uploading.');
							mask.hide();
						}
				});
			}
		} else {
			if (this.getForm().isValid()) {
				Taxamatch.Notice.msg('Notice','One of the three archive methods are required to add archive.');
			} else {
				Taxamatch.Notice.msg('Notice','An archive, database, and authority name is required.');
			}
		}
	}

});